/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.DHTMLConstants;
import model.Vocabulary;

import org.myontology.users.Login;
import org.openrdf.model.URI;

import view.HTMLUIcomponents.BelongsToOM;
import view.HTMLUIcomponents.BroaderThan;
import view.HTMLUIcomponents.ClassesBelongingToOM;
import view.HTMLUIcomponents.ClassesSubclassOfClass;
import view.HTMLUIcomponents.Comment;
import view.HTMLUIcomponents.Domain;
import view.HTMLUIcomponents.Example;
import view.HTMLUIcomponents.Images;
import view.HTMLUIcomponents.InstancesHavingDomainClass;
import view.HTMLUIcomponents.Label;
import view.HTMLUIcomponents.NarrowerThan;
import view.HTMLUIcomponents.PartlyOverlapsWith;
import view.HTMLUIcomponents.PropertiesHavingDomainClass;
import view.HTMLUIcomponents.PropertiesHavingRangeClass;
import view.HTMLUIcomponents.Range;
import view.HTMLUIcomponents.SameAs;
import view.HTMLUIcomponents.SeeAlsos;
import view.HTMLUIcomponents.SubClassOf;
import view.HTMLUIcomponents.Synonyms;
import view.HTMLUIcomponents.Tags;
import view.HTMLUIcomponents.Translations;
import view.HTMLUIcomponents.Videos;
import controller.HelperFunctions;
import controller.elementMapper.ClassMapper;
import controller.elementMapper.Mapper;
import controller.elementMapper.OntologyModuleMapper;
import controller.elementMapper.PropertyMapper;
import controller.sesameDataConnector.AbstractDataConnector;

/**
 * Servlet implementation class for Servlet: GetFormInEditMode
 *
 */
 public class GetFormInEditMode extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 7L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetFormInEditMode() {
		super();
	}   	
	

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		if ( !Login.LOGGED_IN.getValue(request) ) {			
			response.sendRedirect( request.getContextPath() + "/login");
			return;
		}
		
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		
		String ret = "";

		String cssidcontent = request.getParameter( "cssidcontent" ).trim();
		String oldtimestamp = request.getParameter( "oldtimestamp" );
		String viewType = request.getParameter( "viewtype" );
		
		String id = request.getParameter( "id" ).trim();
		boolean shownavigation = false;
		if( request.getParameter( "shownavigation" )!=null && request.getParameter( "shownavigation" ).equals("true") ) {
			shownavigation = true;
		}
		boolean showcreatelinks = shownavigation;

		String form = "";
		String focusAfterReceive = ""; // the id of the HTML-field which should get focus afterwards

		try {
			
			ArrayList<Exception> exceptions = null;

			// URI uri = new URIImpl(id);
			URI uri =AbstractDataConnector.getURI( id );
			URI oldUri = null;
			if(uri!=null)
				oldUri = Mapper.selectVersionUri(uri, oldtimestamp);
			
			if( cssidcontent.equals( Comment.CSSIDCONTENT ) ) {

				String entry = Mapper.selectComment(oldUri, Vocabulary.ENGLISH);
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						Mapper.selectComment(uri, Vocabulary.ENGLISH), 
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Comment.getFormInEditMode(uri, entry, exceptions, shownavigation, Vocabulary.ENGLISH );
				focusAfterReceive = Comment.NAMEFORMELEMENT;
			}
			
			else if( cssidcontent.equals( Tags.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatStrings( Mapper.selectTags(oldUri), HelperFunctions.TAGSSPLITTER );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatStrings( Mapper.selectTags(uri), HelperFunctions.TAGSSPLITTER ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Tags.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Tags.NAMEFORMELEMENT;
			}
			
			else if( cssidcontent.equals( Synonyms.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatStrings( Mapper.selectSynonyms(oldUri), HelperFunctions.TAGSSPLITTER );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatStrings( Mapper.selectSynonyms(uri), HelperFunctions.SYNONYMSPLITTER ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Synonyms.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Synonyms.NAMEFORMELEMENT;
			}
			
			else if( cssidcontent.equals( SeeAlsos.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = Mapper.selectSeeAlso(oldUri);
				String entry = HelperFunctions.concatURIs( oldList );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						Mapper.selectSeeAlso(uri),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = SeeAlsos.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = SeeAlsos.FOCUSIDOFEDITPANEL;
			}
			
			else if( cssidcontent.equals( Example.CSSIDCONTENT ) ) {

				String entry = Mapper.selectExample(oldUri);
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						Mapper.selectExample(uri),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Example.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Example.NAMEFORMELEMENT;
			}
			
			else if( cssidcontent.equals( Translations.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatTranslations( Mapper.selectTranslation(oldUri) );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatTranslations( Mapper.selectTranslation(uri) ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Translations.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Translations.FOCUSIDOFEDITPANEL;
			}
			
			else if( cssidcontent.equals( Images.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatStrings( Mapper.selectImages(oldUri) );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatStrings( Mapper.selectImages(uri) ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Images.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Images.FOCUSIDOFEDITPANEL;
			}
			
			else if( cssidcontent.equals( Videos.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatStrings( Mapper.selectVideos(oldUri) );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatStrings( Mapper.selectVideos(uri) ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Videos.getFormInEditMode(uri, entry, exceptions, shownavigation );
				focusAfterReceive = Videos.FOCUSIDOFEDITPANEL;
			}

			// #############################################################################################
			// #############################################################################################
			
			
			else if( cssidcontent.equals( ClassesBelongingToOM.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = OntologyModuleMapper.selectClassesBelongingToOM(oldUri);
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						OntologyModuleMapper.selectClassesBelongingToOM(uri),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = ClassesBelongingToOM.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = ClassesBelongingToOM.NAMEFORMELEMENT + ClassesBelongingToOM.NAMEFORMELEMENTADDEREXTENSION;
			}
			/*
			else if( cssidcontent.equals( PropertiesBelongingToOM.CSSIDCONTENT ) ) {

				String entry = HelperFunctions.concatURIs( OntologyModuleMapper.selectPropertiesBelongingToOM(oldUri) );
				
				try {
					
					Mapper.checkIfStoredInMeantime( 
						HelperFunctions.concatURIs( OntologyModuleMapper.selectPropertiesBelongingToOM(uri) ),
						entry );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = PropertiesBelongingToOM.getFormInEditMode(uri, entry, exceptions, true );
			}
			*/

			else if( cssidcontent.equals( PartlyOverlapsWith.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = null;

				try {	
					
					if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
						
						oldList = ClassMapper.selectPartlyOverlapsWith(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							ClassMapper.selectPartlyOverlapsWith( uri ),
							oldList );
					}
					else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
						
						oldList = PropertyMapper.selectPartlyOverlapsWith(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							PropertyMapper.selectPartlyOverlapsWith( uri ),
							oldList );
					}
				
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = PartlyOverlapsWith.getFormInEditMode(uri, viewType, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = PartlyOverlapsWith.NAMEFORMELEMENT + PartlyOverlapsWith.NAMEFORMELEMENTADDEREXTENSION;
			}

			else if( cssidcontent.equals( SameAs.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = null;

				try {	

					if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
						
						oldList = ClassMapper.selectSameAs(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							ClassMapper.selectSameAs( uri ),
							oldList );
					}
					else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
						
						oldList = PropertyMapper.selectSameAs(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							PropertyMapper.selectSameAs( uri ),
							oldList );
					}
				
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = SameAs.getFormInEditMode( uri, viewType, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = SameAs.NAMEFORMELEMENT + SameAs.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( NarrowerThan.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = null;

				try {	

					if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
						
						oldList = ClassMapper.selectNarrowerThan(oldUri);

						Mapper.checkIfStoredInMeantime( 
							ClassMapper.selectNarrowerThan( uri ),
							oldList );
					}
					else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
						
						oldList = PropertyMapper.selectNarrowerThan(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							PropertyMapper.selectNarrowerThan( uri ),
							oldList );
					}
				
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = NarrowerThan.getFormInEditMode( uri, viewType, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = NarrowerThan.NAMEFORMELEMENT + NarrowerThan.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( BroaderThan.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = null;

				try {	

					if( viewType.equals( DHTMLConstants.CLASS_VIEWTYPE ) ) {
						
						oldList = ClassMapper.selectBroaderThan(oldUri);

						Mapper.checkIfStoredInMeantime( 
							ClassMapper.selectBroaderThan( uri ),
							oldList );
					}
					else if( viewType.equals( DHTMLConstants.PROPERTY_VIEWTYPE ) ) {
						
						oldList = PropertyMapper.selectBroaderThan(oldUri);
						
						Mapper.checkIfStoredInMeantime( 
							PropertyMapper.selectBroaderThan( uri ),
							oldList );
					}
				
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = BroaderThan.getFormInEditMode( uri, viewType, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = BroaderThan.NAMEFORMELEMENT + BroaderThan.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( SubClassOf.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = ClassMapper.selectSubclassOf(oldUri);
				
				try {	
					
					Mapper.checkIfStoredInMeantime( 
						ClassMapper.selectSubclassOf( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = SubClassOf.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = SubClassOf.NAMEFORMELEMENT + SubClassOf.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( Domain.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = HelperFunctions.extractURIs( oldtimestamp ); // workaround
				// String entry = HelperFunctions.concatURIs( PropertyMapper.selectDomain(oldUri) );
					
				try {			
					
					Mapper.checkIfStoredInMeantime( 
						PropertyMapper.selectDomain( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Domain.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = Domain.NAMEFORMELEMENT + Domain.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( Range.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = PropertyMapper.selectRange(oldUri);
				
				try {					

					Mapper.checkIfStoredInMeantime( 
						PropertyMapper.selectRange( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = Range.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = Range.NAMEFORMELEMENT + Range.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( BelongsToOM.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = HelperFunctions.extractURIs( oldtimestamp ); // workaround
				// String entry = HelperFunctions.concatURIs( PropertyMapper.selectBelongsToOM(oldUri) );
				
				try {				
					
					Mapper.checkIfStoredInMeantime( 
						PropertyMapper.selectBelongsToOM( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = BelongsToOM.getFormInEditMode( uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = BelongsToOM.NAMEFORMELEMENT + BelongsToOM.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( ClassesSubclassOfClass.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = HelperFunctions.extractURIs( oldtimestamp ); // workaround 
				// String entry = HelperFunctions.concatURIs( ClassMapper.selectAllClassesHavingClassInSubClassOf(oldUri) );
				
				try {					
					
					Mapper.checkIfStoredInMeantime( 
						ClassMapper.selectAllClassesHavingClassInSubClassOf( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = ClassesSubclassOfClass.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = ClassesSubclassOfClass.NAMEFORMELEMENT + ClassesSubclassOfClass.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( PropertiesHavingDomainClass.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = ClassMapper.selectAllPropertiesHavingClassInDomain(oldUri);
				
				try {					
					
					Mapper.checkIfStoredInMeantime( 
						ClassMapper.selectAllPropertiesHavingClassInDomain( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = PropertiesHavingDomainClass.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = PropertiesHavingDomainClass.NAMEFORMELEMENT + PropertiesHavingDomainClass.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( InstancesHavingDomainClass.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = ClassMapper.selectAllInstancesHavingClassInDomain(oldUri);
				
				try {					
					
					Mapper.checkIfStoredInMeantime( 
						ClassMapper.selectAllPropertiesHavingClassInDomain( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = InstancesHavingDomainClass.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = InstancesHavingDomainClass.NAMEFORMELEMENT + InstancesHavingDomainClass.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			else if( cssidcontent.equals( PropertiesHavingRangeClass.CSSIDCONTENT ) ) {

				ArrayList<URI> oldList = HelperFunctions.extractURIs( oldtimestamp ); // workaround
				// String entry = HelperFunctions.concatURIs( ClassMapper.selectAllPropertiesHavingClassInRange(oldUri) );
				
				try {					
					
					Mapper.checkIfStoredInMeantime( 
						ClassMapper.selectAllPropertiesHavingClassInRange( uri ),
						oldList );
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				
				form = PropertiesHavingRangeClass.getFormInEditMode(uri, oldList, exceptions, shownavigation, showcreatelinks );
				focusAfterReceive = PropertiesHavingRangeClass.NAMEFORMELEMENT + PropertiesHavingRangeClass.NAMEFORMELEMENTADDEREXTENSION;
			}
			
			// #############################################################################################
			// #############################################################################################
			
			
			else if( cssidcontent.equals( Label.CSSIDCONTENT ) ) {

				/*
				try {		
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				*/
				
				String entry = Mapper.selectLabel( uri, Vocabulary.ENGLISH );	

				form = Label.getFormInEditMode(uri, entry, AbstractDataConnector.getType(uri), exceptions, shownavigation );
				focusAfterReceive = Label.NAMEFORMELEMENT;
			}
			
			else if( cssidcontent.equals( Label.CSSIDCONTENT_TITLE ) ) {

				/*
				try {		
				}
				catch( Exception e ) {
					exceptions = new ArrayList<Exception>();
					exceptions.add( e );
				}
				*/
				
				form = Label.getTitleForm( uri, AbstractDataConnector.getType(uri), shownavigation );
			}
			
			
		}
		catch( Exception e ) {
			
			e.printStackTrace();
		}
		

		

		// --------------------------

		ret = 
			"<handlegetformineditmoderesponse>" + 
				"<" + cssidcontent + ">" + 
					"<![CDATA[" + form + "]]>" +
				"</" + cssidcontent + ">" + 
				"<focusidafterreceive>" + 
					"<![CDATA[" + focusAfterReceive + "]]>" +
				"</focusidafterreceive>" + 
			"</handlegetformineditmoderesponse>";
		
	    PrintWriter out = response.getWriter();

	    out.println("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
	    
	    out.println( ret );		
	}  	  	  	    
}
